private import base;

-- private import atomics;

-- compare and swap
atomic_int_cas has type (atomic_int, _integer, _integer) => _integer
atomic_int_cas(ar, expected, upd) is valof {
  while (true) do {
    var curr := __atomic_int_reference(ar);
    if __atomic_int_test_n_set(ar, expected, upd) then
      valis expected -- expected equals current
    else if not __integer_eq(curr, expected) then
      valis curr;
    -- else loop - value must have changed between deref and swap
    -- logMsg(info, "cas: current was $(__display(curr)), setting from $(__display(expected)) to $(__display(upd)) failed");
  }
}

-- referential equality
atomic_int_ref_eq has type (atomic_int, atomic_int) => boolean
atomic_int_ref_eq(r1, r2) is __equal(r1, r2)